﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using System.IO;

namespace Addin665.Commands {
    /// <summary>
    /// 在代码中添加头部信息
    /// </summary>
    class AddHeaderInfoCmd : CommandBase {
        string _author = "向柳波";

        public AddHeaderInfoCmd() {
            this.Name = "AddHeaderInfoCmd";
        }

        public override void OnClick() {
            try {
                base.OnClick();
                Document doc = base.ApplicationObject.ActiveDocument;
                if (doc == null) {
                    return;
                }
                TextDocument textObj = doc.Object("TextDocument") as TextDocument;
                TextPoint startPoint = textObj.StartPoint;
                EditPoint editPoint = startPoint.CreateEditPoint();

                string code = CreateCode();

                editPoint.Insert(code);
            }
            catch (Exception ex) {
                CommonHelp.LogHelper.Error(ex);
            }
        }


        string CreateCode() {
            string ret = null;

            string fullPath = System.Reflection.Assembly.GetAssembly(typeof(Connect)).Location;
            string cfgFile = System.IO.Path.GetDirectoryName(fullPath) + @"\Data" + @"\AddHeaderInfo.txt";
            if (File.Exists(cfgFile)) {
                StringBuilder sb = new StringBuilder(CommonHelp.FileHelper.ReadFile(cfgFile));
                sb.Replace("$GUID", Guid.NewGuid().ToString());
                sb.Replace("$Domain", Environment.UserDomainName);
                sb.Replace("$SysUser", Environment.UserName);
                sb.Replace("$DateTimeNow", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                sb.AppendLine();
                ret = sb.ToString();
            }
            else {
                ret = CreateDefaultCode();
            }
            return ret;
        }

        string CreateDefaultCode() {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("/************************************************************************");
            sb.AppendLine(@"*  Copyright @   All rights reserved.");
            sb.AppendLine(@"*  GUID: " + Guid.NewGuid().ToString());
            sb.AppendLine(@"*  计算机用户域： " + Environment.UserDomainName);
            sb.AppendLine(@"*  计算机用户名： " + Environment.UserName);
            sb.AppendLine(@"*  创建者： " + _author);
            sb.AppendLine(@"*  创建时间： " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            sb.AppendLine(@"*  创建说明： ");
            sb.AppendLine(@"*  ---------------------------------------------------------------------");
            sb.AppendLine(@"*  更新者： ");
            sb.AppendLine(@"*  更新时间： ");
            sb.AppendLine(@"*  更新说明： ");
            sb.AppendLine("************************************************************************/");
            sb.AppendLine();

            return sb.ToString();
        }
    }
}
